package com.atguigu.web;

import com.atguigu.pojo.*;
import com.atguigu.service.*;
import com.atguigu.service.impl.*;
import com.atguigu.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

public class TaskServlet_bak extends BaseServlet {
    SubtaskService subtaskService=new SubtaskServiceImpl();
    ServiceService serviceService=new ServiceServiceImpl();
    ServerService serverService=new ServerServiceImpl();
    UpdatePhpCodeTaskService updatePhpCodeTaskService=new UpdatePhpCodeTaskServiceImpl();
    NginxServiceConfigPhpService nginxServiceConfigPhpService=new NginxServiceConfigPhpServiceImpl();
    Gson gson=new Gson();
    ProjectService projectService=new ProjectServiceImpl();
    ProjectModuleService projectModuleService=new ProjectModuleServiceImpl();
    ProjectModuleTypeService projectModuleTypeService=new ProjectModuleTypeServiceImpl();
    TomcatServiceConfigService tomcatServiceConfigService=new TomcatServiceConfigServiceImpl();
    TomcatAndHeartbeatServiceConfigService tomcatAndHeartbeatServiceConfigService=new TomcatAndHeartbeatServiceConfigServiceImpl();
    UpdateTomcatCodeTaskService updateTomcatCodeTaskService=new UpdateTomcatCodeTaskServiceImpl();
    UpdateTomcatCodeAndHeartbeatTaskService updateTomcatCodeAndHeartbeatTaskService=new UpdateTomcatCodeAndHeartbeatTaskServiceImpl();
    BasicBehaviorService basicBehaviorService=new BasicBehaviorServiceImpl();
    MainTaskService mainTaskService=new MainTaskServiceImpl();
    DeployServiceModuleService deployServiceModuleService=new DeployServiceModuleServiceImpl();
    DeployGoalService deployGoalService=new DeployGoalServiceImpl();
    UserService userService=new UserServiceImpl();
    DeployEnvService deployEnvService=new DeployEnvServiceImpl();
    NginxServiceConfigProxyService nginxServiceConfigProxyService=new NginxServiceConfigProxyServiceImpl();


    protected void reportSubtaskStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String subtaskId=request.getParameter("subtaskId");
        if(subtaskId==""){
            response.getWriter().write("subtaskId不能为空");
        }
        Subtask subtask=subtaskService.querySubtaskById(subtaskId);
        //System.out.println(taskId);
        if(subtask==null){
            response.getWriter().write("subtaskId无效:"+subtaskId);
            return;
        }

        String status=request.getParameter("status");
        //System.out.println("状态是:"+status);
        subtask.setStatus(status);

        String result=request.getParameter("result");
        //System.out.println("结果是:"+result);
        subtask.setResult(result);
        //System.out.println(subtask);
        subtaskService.updateSubtask(subtask);

        Integer count=subtaskService.querySubtasksByStatusNotTrue();
        if(subtask.getMainTaskType().contains("updatePhpCodeTask")){
            UpdatePhpCodeTask updatePhpCodeTask=updatePhpCodeTaskService.queryUpdatePhpCodeTaskById(subtask.getMainTaskId());
            System.out.printf("本次查看主任务剩余子任务量:"+count);
            if(count==0){
                updatePhpCodeTask.setStatus("true");
                updatePhpCodeTaskService.updateUpdatePhpCodeTask(updatePhpCodeTask);
            }
        }

        response.getWriter().write("结果已收到");
    }
    protected void getSubtaskCommand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String subtaskId=request.getParameter("subtaskId");
        if(subtaskId==""){
            response.getWriter().write("subtaskId不能为空");
        }
        Subtask subtask=subtaskService.querySubtaskById(subtaskId);
        if(subtask==null){
            response.getWriter().write("subtaskId无效:"+subtaskId);
            return;
        }
        //System.out.println(subtask);
        response.getWriter().write(subtask.getCommand());
    }
    protected void getSubtaskNeedDoIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String serverId=request.getParameter("serverId");
        Server server=serverService.queryServerById(serverId);
        if(server==null){
            response.getWriter().write("serverid : "+serverId+" not found");
            return;
        }//排除serverId 无效的情况
        List<Subtask> subtasks=subtaskService.querySubtasksByServerId(serverId);
        String subtaskIds="";
        for(Subtask subtask:subtasks){
            if(subtaskIds==""){
                subtaskIds=subtask.getId();
            }else {
                subtaskIds=subtaskIds+","+subtask.getId();
            }
        }
        //System.out.println(serverId);
        response.getWriter().write(subtaskIds);
        return;
        //response.getWriter().write("hello");
    }
    protected void getSubtaskIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String serverId=request.getParameter("serverId");
        Server server=serverService.queryServerById(serverId);
        if(server==null){
            response.getWriter().write("serverid : "+serverId+" not found");
            return;
        }//排除serverId 无效的情况
        List<Subtask> subtasks=subtaskService.queryNeedDoSubtasksByServerId(serverId);
        //System.out.println(serverId);
        String subtaskIds="";
        for(Subtask subtask:subtasks){
            if(subtaskIds==""){
                subtaskIds=subtask.getId();
            }else {
                subtaskIds=subtaskIds+","+subtask.getId();
            }
            subtask.setStatus("wait");
            subtaskService.updateSubtask(subtask);
        }
        //System.out.println(serverId);
        response.getWriter().write(subtaskIds);
        return;
        //response.getWriter().write("hello");
    }
    protected void addTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //System.out.println(request.getSession().getAttribute("user"));
        if (request.getMethod().equals("GET")){
            List<UpdatePhpCodeTask> updatePhpCodeTasks=updatePhpCodeTaskService.queryUpdatePhpCodeTasks();
            request.setAttribute("tasks",updatePhpCodeTasks); //这一个会被用在:"第一次打开时,左边列表渲染"
            request.setAttribute("taskType","updatePhpCodeTask");//也是给列表用的,将来会在点击任务名时候带上

            ////////////////////////////////////// php project module target 收集区间
            String phpProjectModuleTypeId=projectModuleTypeService.queryProjectModuleTypeByName("php").getId();//获得模块类型为php的类型id,用这个id查找全部符合的模块,从而得到适合php类型的project列表
            List<ProjectModule> projectModulesAllPhp=projectModuleService.queryProjectModulesByProjectModuleTypeId(phpProjectModuleTypeId);
            LinkedHashSet<Project> phpProjects=new LinkedHashSet<>();
            for(ProjectModule projectModule:projectModulesAllPhp){
                Project project=projectService.queryProjectByProjectId(projectModule.getProjectId());
                phpProjects.add(project);
            }
            request.setAttribute("phpProjects",phpProjects);

            String firstProjectId=phpProjects.iterator().next().getId();
            List<ProjectModule> phpProjectModules=projectModuleService.queryProjectModulesByProjectIdAndProjectModuleTypeId(firstProjectId,phpProjectModuleTypeId);
            request.setAttribute("phpProjectModules",phpProjectModules);

            String firstPhpProjectModuleId=phpProjectModules.iterator().next().getId();
            List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByProjectModuleId(firstPhpProjectModuleId);
            request.setAttribute("nginxServiceConfigPhps",nginxServiceConfigPhps);//这个会被用在:"上传php代码form的node节点渲染处"
            ////////////////////////////////////// php project module target 收集区间
            ////////////////////////////////////// tomcat project module target 收集区间
            String tomcatProjectModuleTypeId=projectModuleTypeService.queryProjectModuleTypeByName("tomcat").getId();
            LinkedHashSet<Project> tomcatProjects=projectService.queryProjectsByModuleTypeName("tomcat");
            request.setAttribute("tomcatProjects",tomcatProjects);

            String firstTomcatProjectId=tomcatProjects.iterator().next().getId();
            List<ProjectModule> tomcatProjectModules=projectModuleService.queryProjectModulesByProjectIdAndProjectModuleTypeId(firstTomcatProjectId,tomcatProjectModuleTypeId);
            request.setAttribute("tomcatProjectModules",tomcatProjectModules);

            String firstTomcatProjectModuleId=tomcatProjectModules.iterator().next().getId();
            List<TomcatServiceConfig> tomcatServiceConfigs=tomcatServiceConfigService.queryTomcatServiceConfigsByProjectModuleId(firstTomcatProjectModuleId);
            request.setAttribute("tomcatServiceConfigs",tomcatServiceConfigs);
            ////////////////////////////////////// tomcat project module target 收集区间
            ////////////////////////////////////// tomcatAndHeartbeat project module target 收集区间
            String tomcatAndHeartbeatProjectModuleTypeId=projectModuleTypeService.queryProjectModuleTypeByName("tomcatAndHeartbeat").getId();
            LinkedHashSet<Project> tomcatAndHeartbeatProjects=projectService.queryProjectsByModuleTypeName("tomcatAndHeartbeat");
            request.setAttribute("tomcatAndHeartbeatProjects",tomcatAndHeartbeatProjects);

            String firstTomcatAndHeartbeatProjectId=tomcatAndHeartbeatProjects.iterator().next().getId();
            List<ProjectModule> tomcatAndHeartbeatProjectModules=projectModuleService.queryProjectModulesByProjectIdAndProjectModuleTypeId(firstTomcatAndHeartbeatProjectId,tomcatAndHeartbeatProjectModuleTypeId);
            request.setAttribute("tomcatAndHeartbeatProjectModules",tomcatAndHeartbeatProjectModules);

            String firstTomcatAndHeartbeatProjectModuleId=tomcatAndHeartbeatProjectModules.iterator().next().getId();
            List<TomcatAndHeartbeatServiceConfig> tomcatAndHeartbeatServiceConfigs=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigsByProjectModuleId(firstTomcatAndHeartbeatProjectModuleId);
            request.setAttribute("tomcatAndHeartbeatServiceConfigs",tomcatAndHeartbeatServiceConfigs);
            ////////////////////////////////////// tomcatAndHeartbeat project module target 收集区间
            ////////////////////////////////////// user 收集区间
            List<User> users= userService.queryUsers();
            request.setAttribute("users",users);
            ////////////////////////////////////// user 收集区间
            ////////////////////////////////////// 时间 收集区间
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String date=df.format(new Date());// new Date()为获取当前系统时间
            request.setAttribute("date",date);
            ////////////////////////////////////// 时间 收集区间
            request.getRequestDispatcher("/pages/client/task.jsp").forward(request,response);
        }else if (request.getMethod().equals("POST")){
            String taskType=request.getParameter("taskType");
            if (taskType.equals("updatePhpCodeTask")){
                String userId=request.getParameter("userId");
                String taskName=request.getParameter("taskName");
                String time=request.getParameter("time");
                String files=request.getParameter("files");
                if (files.equals("")){
                    response.getWriter().write("files不能为空");
                    return;
                }//解决文件列表为空的问题
                String projectTypeId=request.getParameter("projectType");
                String projectModuleId=request.getParameter("projectModule");
                String mainTaskId=request.getParameter("mainTaskId");

                UpdatePhpCodeTask updatePhpCodeTask=new UpdatePhpCodeTask();
                if(mainTaskId==null){//新增
                    updatePhpCodeTask=new UpdatePhpCodeTask(WebUtils.id(),userId,taskName,time,files,projectTypeId,projectModuleId,"make");//创建了一个主任务,这个主任务,应该有个状态
                    // make     制订
                    // permit   测试放行
                    // true    执行ok
                    // false   执行失败
                    // pause   暂停执行
                    // cancel   取消
                    updatePhpCodeTaskService.addUpdatePhpCodeTask(updatePhpCodeTask);
                }else {//修改
                    updatePhpCodeTask=updatePhpCodeTaskService.queryUpdatePhpCodeTaskById(mainTaskId);
                    if(updatePhpCodeTask==null){
                        response.getWriter().write("没有这个任务id:"+mainTaskId);
                        return;
                    }else {
                        updatePhpCodeTask.setUserId(userId);
                        updatePhpCodeTask.setName(taskName);
                        updatePhpCodeTask.setTime(time);
                        updatePhpCodeTask.setFiles(files);
                        updatePhpCodeTask.setProjectType(projectTypeId);
                        updatePhpCodeTask.setProjectModule(projectModuleId);
                        updatePhpCodeTask.setStatus("make");
                        subtaskService.deleteSubtaskByMainTaskId(mainTaskId);
                    }
                }//主任务处理逻辑

                ProjectModule projectModule=projectModuleService.queryProjectModuleById(projectModuleId);

                String targetNodes=request.getParameter("targetNodes");

                if(targetNodes.equals("")){
                    response.getWriter().write("必须选择某个节点");
                    return;
                }
                String[] targetNodeArray=targetNodes.split(",");

                for(Integer i=0;i<targetNodeArray.length;i++){

                    NginxServiceConfigPhp nginxServiceConfigPhp=nginxServiceConfigPhpService.queryNginxServiceConfigPhpById(targetNodeArray[i]);
                    String workDirectory=nginxServiceConfigPhp.getWorkDirectory();

                    String[] filesList=files.split("\n");
                    for (int j=0;j<filesList.length;j++){
                        String file=filesList[j];

                        String fileDir="";
                        String fileName="";
                        if(file.contains("/")){
                            fileDir=file.substring(0,file.lastIndexOf("/"));
                            fileName=file.substring(file.lastIndexOf("/")+1);
                            //System.out.println(file);
                            //System.out.println(file.lastIndexOf("/"));
                            //System.out.println(fileDir);
                        }else {
                            fileDir="";
                            fileName=file;
                        }

                        String checkOutUrl=projectModuleService.queryProjectModuleById(nginxServiceConfigPhp.getProjectModuleId()).getDeployPullRootUrl();

                        String command="mv "+WebUtils.addSeparatorChar(workDirectory)+filesList[j]+" "+WebUtils.addSeparatorChar(workDirectory)+filesList[j]+"-"+WebUtils.id();
                        Subtask subtask=new Subtask(WebUtils.id(),updatePhpCodeTask.getId(),"updatePhpCodeTask",nginxServiceConfigPhp.getServerId(),nginxServiceConfigPhp.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                        // make    制订
                        // permit  测试放行
                        // wait    取走,等待执行
                        // doing   正在执行
                        // true    执行ok
                        // false   执行失败
                        // pause   暂停执行
                        // cancel  取消
                        //System.out.println(subtask);
                        subtaskService.addSubtask(subtask);

                        command="wget "+WebUtils.addSeparatorChar(checkOutUrl)+filesList[j]+" -O "+WebUtils.addSeparatorChar(workDirectory)+filesList[j];
                        subtask=new Subtask(WebUtils.id(),updatePhpCodeTask.getId(),"updatePhpCodeTask",nginxServiceConfigPhp.getServerId(),nginxServiceConfigPhp.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                        // make    制订
                        // permit  测试放行
                        // wait    取走,等待执行
                        // doing   正在执行
                        // true    执行ok
                        // false   执行失败
                        // pause   暂停执行
                        // cancel  取消
                        //System.out.println(subtask);
                        subtaskService.addSubtask(subtask);
                        //System.out.println("targetNodes is:" +targetNodes+":"+command);
                    }
                }//子任务处理逻辑
                response.getWriter().write("存入成功");
                return;
                //LinkedHashMap linkedHashMap=new LinkedHashMap();
                //linkedHashMap.put("mainTaskId",updatePhpCodeTask.getId());
                //linkedHashMap.put("status","存入成功");
                //response.getWriter().write(gson.toJson(linkedHashMap));
            }
            if (taskType.equals("updateTomcatCodeTask")){
                String taskName=request.getParameter("taskName");
                String file=request.getParameter("file");
                if (file.equals("")){
                    response.getWriter().write("file不能为空");
                    return;
                }//解决文件列表为空的问题
                String projectTypeId=request.getParameter("projectType");
                String projectModuleId=request.getParameter("projectModule");
                String mainTaskId=request.getParameter("mainTaskId");

                UpdateTomcatCodeTask updateTomcatCodeTask=new UpdateTomcatCodeTask();
                if(mainTaskId==null){//新增
                    updateTomcatCodeTask=new UpdateTomcatCodeTask(WebUtils.id(),taskName,file,projectTypeId,projectModuleId,"make");//创建了一个主任务,这个主任务,应该有个状态
                    // make     制订
                    // permit   测试放行
                    // true    执行ok
                    // false   执行失败
                    // pause   暂停执行
                    // cancel   取消
                    updateTomcatCodeTaskService.addUpdateTomcatCodeTask(updateTomcatCodeTask);
                }else {//修改
                    updateTomcatCodeTask=updateTomcatCodeTaskService.queryUpdateTomcatCodeTaskById(mainTaskId);
                    if(updateTomcatCodeTask==null){
                        response.getWriter().write("没有这个任务id:"+mainTaskId);
                        return;
                    }else {
                        updateTomcatCodeTask.setName(taskName);
                        updateTomcatCodeTask.setFile(file);
                        updateTomcatCodeTask.setProjectType(projectTypeId);
                        updateTomcatCodeTask.setProjectModule(projectModuleId);
                        updateTomcatCodeTask.setStatus("make");
                        subtaskService.deleteSubtaskByMainTaskId(mainTaskId);
                    }
                }//主任务处理逻辑

                ProjectModule projectModule=projectModuleService.queryProjectModuleById(projectModuleId);

                String targetNodes=request.getParameter("targetNodes");

                if(targetNodes.equals("")){
                    response.getWriter().write("必须选择某个节点");
                    return;
                }
                String[] targetNodeArray=targetNodes.split(",");

                for(Integer i=0;i<targetNodeArray.length;i++){
                    //为每个 targetNode 生成任务
                    TomcatServiceConfig tomcatServiceConfig=tomcatServiceConfigService.queryTomcatServiceConfigById(targetNodeArray[i]);
                    String downloadUrl=WebUtils.addSeparatorChar(projectModule.getDeployPullRootUrl())+file;
                    String downloadPath=WebUtils.addSeparatorChar(tomcatServiceConfig.getTomcatHome())+"webApps/"+WebUtils.getFileName(file);
                    String tomcatWorDirRoot=WebUtils.addSeparatorChar(tomcatServiceConfig.getTomcatHome())+"webApps/Root";
                    String catalinaPath=WebUtils.addSeparatorChar(tomcatServiceConfig.getTomcatHome())+"bin/catalina.sh";

                    String command="wget "+downloadUrl+" -O "+downloadPath;
                    Subtask subtask=new Subtask(WebUtils.id(),updateTomcatCodeTask.getId(),"updateTomcatCodeTask",tomcatServiceConfig.getServerId(),tomcatServiceConfig.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                    // make    制订
                    // permit  测试放行
                    // wait    取走,等待执行
                    // doing   正在执行
                    // true    执行ok
                    // false   执行失败
                    // pause   暂停执行
                    // cancel  取消
                    subtaskService.addSubtask(subtask);//下载任务
                    command="mv "+tomcatWorDirRoot+" "+tomcatWorDirRoot+WebUtils.id();
                    subtask=new Subtask(WebUtils.id(),updateTomcatCodeTask.getId(),"updateTomcatCodeTask",tomcatServiceConfig.getServerId(),tomcatServiceConfig.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                    subtaskService.addSubtask(subtask);//备份任务

                    command="unzip -oq "+downloadPath+" -d "+tomcatWorDirRoot;
                    subtask=new Subtask(WebUtils.id(),updateTomcatCodeTask.getId(),"updateTomcatCodeTask",tomcatServiceConfig.getServerId(),tomcatServiceConfig.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                    subtaskService.addSubtask(subtask);//解压任务

                    command=catalinaPath+" stop;sleep 15; "+catalinaPath+" start";
                    subtask=new Subtask(WebUtils.id(),updateTomcatCodeTask.getId(),"updateTomcatCodeTask",tomcatServiceConfig.getServerId(),tomcatServiceConfig.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                    subtaskService.addSubtask(subtask);//重启服务
                }//子任务处理逻辑
                response.getWriter().write("存入成功");
                return;
            }
            if (taskType.equals("updateTomcatCodeAndHeartbeatTask")){
                String taskName=request.getParameter("taskName");
                String file=request.getParameter("file");
                if (file.equals("")){
                    response.getWriter().write("file不能为空");
                    return;
                }//解决文件列表为空的问题
                String projectTypeId=request.getParameter("projectType");
                String projectModuleId=request.getParameter("projectModule");
                String mainTaskId=request.getParameter("mainTaskId");

                UpdateTomcatCodeAndHeartbeatTask updateTomcatCodeAndHeartbeatTask=new UpdateTomcatCodeAndHeartbeatTask();
                if(mainTaskId==null){//新增
                    updateTomcatCodeAndHeartbeatTask=new UpdateTomcatCodeAndHeartbeatTask(WebUtils.id(),taskName,file,projectTypeId,projectModuleId,"make");//创建了一个主任务,这个主任务,应该有个状态
                    // make     制订
                    // permit   测试放行
                    // true    执行ok
                    // false   执行失败
                    // pause   暂停执行
                    // cancel   取消
                    updateTomcatCodeAndHeartbeatTaskService.addUpdateTomcatCodeAndHeartbeatTask(updateTomcatCodeAndHeartbeatTask);
                }else {//修改
                    updateTomcatCodeAndHeartbeatTask=updateTomcatCodeAndHeartbeatTaskService.queryUpdateTomcatCodeAndHeartbeatTaskById(mainTaskId);
                    if(updateTomcatCodeAndHeartbeatTask==null){
                        response.getWriter().write("没有这个任务id:"+mainTaskId);
                        return;
                    }else {
                        updateTomcatCodeAndHeartbeatTask.setName(taskName);
                        updateTomcatCodeAndHeartbeatTask.setFile(file);
                        updateTomcatCodeAndHeartbeatTask.setProjectType(projectTypeId);
                        updateTomcatCodeAndHeartbeatTask.setProjectModule(projectModuleId);
                        updateTomcatCodeAndHeartbeatTask.setStatus("make");
                        subtaskService.deleteSubtaskByMainTaskId(mainTaskId);
                    }
                }//主任务处理逻辑

                ProjectModule projectModule=projectModuleService.queryProjectModuleById(projectModuleId);

                String targetNodes=request.getParameter("targetNodes");

                if(targetNodes.equals("")){
                    response.getWriter().write("必须选择某个节点");
                    return;
                }
                String[] targetNodeArray=targetNodes.split(",");

                for(Integer i=0;i<targetNodeArray.length;i++){
                    //为每个 targetNode 生成任务
                    TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigById(targetNodeArray[i]);
                    String downloadUrl=WebUtils.addSeparatorChar(projectModule.getDeployPullRootUrl())+file;
                    String downloadPath=WebUtils.addSeparatorChar(tomcatAndHeartbeatServiceConfig.getTomcatHome())+"webApps/"+WebUtils.getFileName(file);
                    String tomcatWorDirRoot=WebUtils.addSeparatorChar(tomcatAndHeartbeatServiceConfig.getTomcatHome())+"webApps/Root";
                    String catalinaPath=WebUtils.addSeparatorChar(tomcatAndHeartbeatServiceConfig.getTomcatHome())+"bin/catalina.sh";

                    String command="wget "+downloadUrl+" -O "+downloadPath;
                    Subtask subtask=new Subtask(WebUtils.id(),updateTomcatCodeAndHeartbeatTask.getId(),"updateTomcatCodeTask",tomcatAndHeartbeatServiceConfig.getServerId(),tomcatAndHeartbeatServiceConfig.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                    // make    制订
                    // permit  测试放行
                    // wait    取走,等待执行
                    // doing   正在执行
                    // true    执行ok
                    // false   执行失败
                    // pause   暂停执行
                    // cancel  取消
                    subtaskService.addSubtask(subtask);//下载任务
                    command="mv "+tomcatWorDirRoot+" "+tomcatWorDirRoot+WebUtils.id();
                    subtask=new Subtask(WebUtils.id(),updateTomcatCodeAndHeartbeatTask.getId(),"updateTomcatCodeTask",tomcatAndHeartbeatServiceConfig.getServerId(),tomcatAndHeartbeatServiceConfig.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                    subtaskService.addSubtask(subtask);//备份任务

                    command="unzip -oq "+downloadPath+" -d "+tomcatWorDirRoot;
                    subtask=new Subtask(WebUtils.id(),updateTomcatCodeAndHeartbeatTask.getId(),"updateTomcatCodeTask",tomcatAndHeartbeatServiceConfig.getServerId(),tomcatAndHeartbeatServiceConfig.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                    subtaskService.addSubtask(subtask);//解压任务

                    command=catalinaPath+" stop;sleep 15; "+catalinaPath+" start";
                    subtask=new Subtask(WebUtils.id(),updateTomcatCodeAndHeartbeatTask.getId(),"updateTomcatCodeTask",tomcatAndHeartbeatServiceConfig.getServerId(),tomcatAndHeartbeatServiceConfig.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                    subtaskService.addSubtask(subtask);//重启服务

                    String httpPort=tomcatAndHeartbeatServiceConfig.getHttpPort();
                    String url="http://127.0.0.1:"+httpPort+"/"+tomcatAndHeartbeatServiceConfig.getHeartbeat();
                    command="curl "+url;
                    subtask=new Subtask(WebUtils.id(),updateTomcatCodeAndHeartbeatTask.getId(),"updateTomcatCodeTask",tomcatAndHeartbeatServiceConfig.getServerId(),tomcatAndHeartbeatServiceConfig.getId(),command,"make","");//创建一个子任务,这个子任务,应该有个状态
                    subtaskService.addSubtask(subtask);//心跳检测
                }//子任务处理逻辑
                response.getWriter().write("存入成功");
                return;
            }
        }
    }
    protected void getTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String taskId=request.getParameter("taskId");
        String taskType=request.getParameter("taskType");
        if (taskType.equals("updatePhpCodeTask")){
            UpdatePhpCodeTask updatePhpCodeTask=updatePhpCodeTaskService.queryUpdatePhpCodeTaskById(taskId);
            if(updatePhpCodeTask==null){
                response.getWriter().write("没有这个taskId: "+taskId);
            }else {
                response.getWriter().write(gson.toJson(updatePhpCodeTask));
            }
        }
        if (taskType.equals("updateTomcatCodeTask")){
            UpdateTomcatCodeTask updateTomcatCodeTask=updateTomcatCodeTaskService.queryUpdateTomcatCodeTaskById(taskId);
            if(updateTomcatCodeTask==null){
                response.getWriter().write("没有这个taskId: "+taskId);
            }else {
                response.getWriter().write(gson.toJson(updateTomcatCodeTask));
            }
        }
        if (taskType.equals("updateTomcatCodeAndHeartbeatTask")){
            UpdateTomcatCodeTask updateTomcatCodeTask=updateTomcatCodeTaskService.queryUpdateTomcatCodeTaskById(taskId);
            if(updateTomcatCodeTask==null){
                response.getWriter().write("没有这个taskId: "+taskId);
            }else {
                response.getWriter().write(gson.toJson(updateTomcatCodeTask));
            }
        }
    }
    protected void existBasicBehavior(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //client来请求是否有任务,要看父环节表
        //总任务表规定了,本次要发布的全部服务和是否并行,
        //父环节表规定了api服务的发布过程
        //主任务规定了下线的步骤
        //基础行为规定了服务器具体的实施过程
        String clientId=request.getParameter("clientId");
        //System.out.println(clientId);
        BasicBehavior basicBehavior=basicBehaviorService.queryBasicBehaviorByStatusMakeAndClientId(clientId);
        if(basicBehavior==null){
            response.getWriter().write("no");
        }else {
            response.getWriter().write("yes");
        }
    }
    protected void getStatusMakeBasicBehavior(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String clientId=request.getParameter("clientId");
        String mainTaskId=request.getParameter("mainTaskId");
        //System.out.println(mainTaskId);

        if (mainTaskId.equals("")){//第一次来请求"基础行为"
            BasicBehavior basicBehavior=basicBehaviorService.queryBasicBehaviorByStatusMakeAndClientId(clientId);
            basicBehavior.setStatus("doing");
            basicBehaviorService.updateBasicBehavior(basicBehavior);
            response.getWriter().write(gson.toJson(basicBehavior));
        }else {//有主任务号:第二次或第2+次
            MainTask mainTask=mainTaskService.queryMainTaskById(mainTaskId);
            //System.out.println(mainTask);
            if(mainTask.getStatus().equals("make")) {
                BasicBehavior basicBehavior = basicBehaviorService.queryBasicBehaviorByStatusMakeAndClientIdAndMainTaskId(clientId, mainTaskId);//第二次来请求"基础行为"
                basicBehavior.setStatus("doing");
                basicBehaviorService.updateBasicBehavior(basicBehavior);
                response.getWriter().write(gson.toJson(basicBehavior));
            }else {
                //System.out.println(mainTask.getStatus());
                if (mainTask.getStatus().equals("ok")){
                    //System.out.println(mainTask.getId());
                    MainTask newMainTask=mainTaskService.queryMainTaskByLastId(mainTask.getId());
                    //System.out.println("新的主任务"+newMainTask);//下一个"mainTask"的"基础行为"为null,说明:"deployModule"没有主任务了,应该用当下的deployModule,查下一个deployModule,从而查出下一个deployModule的"基础行为"
                    BasicBehavior basicBehavior=new BasicBehavior();
                    if(newMainTask==null){
                        DeployServiceModule newDeployServiceModule=deployServiceModuleService.queryDeployServiceModuleByLastId(mainTask.getDeployServiceModuleId());
                        //System.out.println(newDeployServiceModule);
                        basicBehavior = basicBehaviorService.queryBasicBehaviorByStatusMakeAndClientIdAndDeployServiceModuleId(clientId, newDeployServiceModule.getId());
                    }else {
                        basicBehavior = basicBehaviorService.queryBasicBehaviorByStatusMakeAndClientIdAndMainTaskId(clientId, newMainTask.getId());//这个"mainTask",搜索下一个"mainTask"的"基础行为"
                    }
                    //System.out.println(basicBehavior);
                    basicBehavior.setStatus("doing");
                    basicBehaviorService.updateBasicBehavior(basicBehavior);
                    response.getWriter().write(gson.toJson(basicBehavior));
                }
            }
        }
    }
    protected void reportBasicBehavior(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id=request.getParameter("id");
        String status=request.getParameter("status");
        String result=request.getParameter("result");
        //System.out.println(id+":"+status+":"+result);

        BasicBehavior basicBehavior=basicBehaviorService.queryBasicBehaviorById(id);
        if(basicBehavior==null){
            response.getWriter().write("没有这个id:"+id);
            return;
        }else {
            if(status.equals("0")){
                basicBehavior.setStatus("ok");
                basicBehavior.setResult(result);
                basicBehaviorService.updateBasicBehavior(basicBehavior);
                //这一条已经执行完了,要看看主任务上是否还有操作
                String mainTaskId=basicBehavior.getMainTaskId();
                MainTask mainTask=mainTaskService.queryMainTaskById(mainTaskId);
                String mainTaskName=mainTask.getName();

                //这一条已经执行完了,要看看主任务上是否还有操作
                Method method= null;
                try {
                    method = this.getClass().getDeclaredMethod(mainTaskName,BasicBehavior.class);//进入"主任务,多基础行为"环节###################################
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
                try {
                    assert method != null;
                    String yesOrNo=(String) method.invoke(this,basicBehavior);//this表示当前的实例对象
                    if (yesOrNo.equals("yes")){
                        response.getWriter().write(yesOrNo);
                        return;
                    }else if (yesOrNo.equals("no")){
                        mainTask.setStatus("ok");
                        mainTaskService.updateMainTask(mainTask);
                        //执行完这一条,说明这个主任务执行完了,要去deployServiceModule看看,是否还有操作
                        DeployServiceModule deployServiceModule=deployServiceModuleService.queryDeployServiceModuleById(mainTask.getDeployServiceModuleId());
                        String projectModuleId=deployServiceModule.getProjectModuleId();
                        ProjectModule projectModule=projectModuleService.queryProjectModuleById(projectModuleId);
                        ProjectModuleType projectModuleType=projectModuleTypeService.queryProjectModuleTypeById(projectModule.getProjectModuleTypeId());
                        String projectModuleTypeName=projectModuleType.getName();
                        try {
                            method = this.getClass().getDeclaredMethod(projectModuleTypeName,MainTask.class);//进入"发布不同服务类型,多主任务"环节######################################
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        }
                        yesOrNo=(String) method.invoke(this,mainTask);//this表示当前的实例对象
                        if (yesOrNo.equals("yes")){//这个模块还有主任务要后续,那就继续
                            response.getWriter().write(yesOrNo);
                            return;
                        }else {//这个模块完成了,修改发布模块状态,检查发布列表是否还有其他模块要发布
                            deployServiceModule.setStatus("ok");
                            deployServiceModuleService.updateDeployServiceModule(deployServiceModule);
                            //执行到这里,说明这个服务发布好了,要去deployServiceModule看看是否还有后续服务要发布
                            DeployServiceModule newDeployServiceModule=deployServiceModuleService.queryDeployServiceModuleByLastId(deployServiceModule.getId());
                            if (newDeployServiceModule==null){
                                //没有新的服务需要部署了,可以修改deployGoal的状态了,并回复客户端,没有了
                                DeployGoal deployGoal=deployGoalService.queryDeployGoalById(deployServiceModule.getDeployGoalId());
                                deployGoal.setStatus("ok");
                                deployGoalService.updateDeployGoal(deployGoal);
                                response.getWriter().write("no");//发布任务终止
                                return;
                            }else {/////////////////////////////////////////////////////////////////// 进入 一个发布清单,有多个服务模块需要发布,根据"deployServiceModule",按服务模块的发布类型,生成"mainTask","basicBehavior"#########################
                                //进入到这里,说明,还有新服务,需要部署
                                String newProjectModuleId=newDeployServiceModule.getProjectModuleId();
                                ProjectModule newProjectModule=projectModuleService.queryProjectModuleById(newProjectModuleId);
                                String newProjectModuleTypeId=newProjectModule.getProjectModuleTypeId();
                                ProjectModuleType newProjectModuleType=projectModuleTypeService.queryProjectModuleTypeById(newProjectModuleTypeId);
                                if(newProjectModuleType.getName().equals("tomcatAndHeartbeat")){//如果这个模块是springboot的格式,还要单独写一次 "mainTask","basicBehavior"
                                    //生成 "tomcatAndHeartbeat" 类型的一系列任务中的第一个 "offline" ,并顺便生成 第一个 "basicBehavior"
                                    MainTask newMainTask=new MainTask();
                                    newMainTask.setId(WebUtils.id());
                                    newMainTask.setDeployServiceModuleId(newDeployServiceModule.getId());
                                    newMainTask.setDeployGoalId(newDeployServiceModule.getDeployGoalId());
                                    newMainTask.setName("offline");
                                    newMainTask.setStatus("make");
                                    newMainTask.setLastId("0");
                                    mainTaskService.addMainTask(newMainTask);

                                    BasicBehavior newBasicBehavior=new BasicBehavior();
                                    newBasicBehavior.setId(WebUtils.id());
                                    newBasicBehavior.setMainTaskId(newMainTask.getId());
                                    newBasicBehavior.setDeployServiceModuleId(newDeployServiceModule.getId());
                                    newBasicBehavior.setDeployGoalId(newDeployServiceModule.getDeployGoalId());

                                    TomcatAndHeartbeatServiceConfig newTomcatAndHeartbeatServiceConfig=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigById(newDeployServiceModule.getTargetNodeId());
                                    Server newServer=serverService.queryServerById(newTomcatAndHeartbeatServiceConfig.getServerId());
                                    String ip=newServer.getIp();
                                    String httpPort=newTomcatAndHeartbeatServiceConfig.getHttpPort();
                                    NginxServiceConfigProxy newNginxServiceConfigProxy=nginxServiceConfigProxyService.queryNginxServiceConfigProxyByTargetNodeId(newTomcatAndHeartbeatServiceConfig.getId());
                                    String domain=newNginxServiceConfigProxy.getFrontDomain();
                                    String newNginxConfigVhostFile="/lhwl/app/nginx/conf/vhost/"+domain+".conf";
                                    String command="grep -n "+ip+":"+httpPort+" "+newNginxConfigVhostFile;

                                    newBasicBehavior.setCommand(command);
                                    newBasicBehavior.setClientId(newServer.getId());
                                    newBasicBehavior.setResult("");
                                    newBasicBehavior.setStatus("make");
                                    newBasicBehavior.setType("grep");
                                    basicBehaviorService.addBasicBehavior(newBasicBehavior);
                                }//如果这个模块是springboot的格式,还要单独写一次 "mainTask","basicBehavior" !!!!!!!!!!!!!!!!!!!!!
                                response.getWriter().write("yes");//提示客户端还有新任务
                                return;
                            }
                        }
                    }else if(yesOrNo.equals("error")){
                        basicBehavior.setStatus("error");
                        basicBehaviorService.updateBasicBehavior(basicBehavior);

                        mainTask=mainTaskService.queryMainTaskById(basicBehavior.getMainTaskId());
                        mainTask.setStatus("error");
                        mainTaskService.updateMainTask(mainTask);

                        DeployServiceModule deployServiceModule=deployServiceModuleService.queryDeployServiceModuleById(basicBehavior.getDeployServiceModuleId());
                        deployServiceModule.setStatus("error");
                        deployServiceModuleService.updateDeployServiceModule(deployServiceModule);

                        DeployGoal deployGoal=deployGoalService.queryDeployGoalById(basicBehavior.getDeployGoalId());
                        deployGoal.setStatus("error");
                        deployGoalService.updateDeployGoal(deployGoal);

                        response.getWriter().write(yesOrNo);//提示客户端上一条执行状态ok,但是result结果判定异常:error
                        return;
                    }
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }else{
                basicBehavior.setStatus("error");
                basicBehavior.setResult(result);
                basicBehaviorService.updateBasicBehavior(basicBehavior);

                MainTask mainTask=mainTaskService.queryMainTaskById(basicBehavior.getMainTaskId());
                mainTask.setStatus("error");
                mainTaskService.updateMainTask(mainTask);

                DeployServiceModule deployServiceModule=deployServiceModuleService.queryDeployServiceModuleById(basicBehavior.getDeployServiceModuleId());
                deployServiceModule.setStatus("error");
                deployServiceModuleService.updateDeployServiceModule(deployServiceModule);

                DeployGoal deployGoal=deployGoalService.queryDeployGoalById(basicBehavior.getDeployGoalId());
                deployGoal.setStatus("error");
                deployGoalService.updateDeployGoal(deployGoal);
            }
            response.getWriter().write("没有新任务了");
        }
    }
    protected void getTasks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String taskType=request.getParameter("taskType");
        //System.out.println(taskType);
        if (taskType.equals("updatePhpCodeTask")){
            List <UpdatePhpCodeTask> updatePhpCodeTasks=updatePhpCodeTaskService.queryUpdatePhpCodeTasks();
            response.getWriter().write(gson.toJson(updatePhpCodeTasks));
        }
        if (taskType.equals("updateTomcatCodeTask")){
            List <UpdateTomcatCodeTask> updateTomcatCodeTasks=updateTomcatCodeTaskService.queryUpdateTomcatCodeTasks();
            response.getWriter().write(gson.toJson(updateTomcatCodeTasks));
        }
        if (taskType.equals("updateTomcatCodeAndHeartbeatTask")){
            List <UpdateTomcatCodeAndHeartbeatTask> updateTomcatCodeAndHeartbeatTasks=updateTomcatCodeAndHeartbeatTaskService.queryUpdateTomcatCodeAndHeartbeatTasks();
            //System.out.println(updateTomcatCodeTasks);
            response.getWriter().write(gson.toJson(updateTomcatCodeAndHeartbeatTasks));
        }
    }
    protected void getTaskTargetNodes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //返回2组数据,1组是该任务的模块的全部nodes,用来打底;2组是该任务选中的nodes
        String taskId=request.getParameter("taskId");
        String taskType=request.getParameter("taskType");
        //System.out.println(taskType+","+taskId);
        LinkedHashMap linkedHashMap=new LinkedHashMap();

        if(taskType.equals("updatePhpCodeTask")){
            ////////////////////////获取任务内容区间
            UpdatePhpCodeTask task=updatePhpCodeTaskService.queryUpdatePhpCodeTaskById(taskId);
            linkedHashMap.put("task",task);
            ////////////////////////获取任务内容区间
            ////////////////////////获取任务涉及模块存在的node的区间
            List allNodes=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByProjectModuleId(task.getProjectModule());
            linkedHashMap.put("allNodes",allNodes);
            ////////////////////////获取任务涉及模块存在的node的区间
            ////////////////////////获取任务涉及的node区间
            List<Subtask> subtasks =subtaskService.queryNeedDoSubtasksByMainTaskId(taskId);
            LinkedHashSet<String> targetIds=new LinkedHashSet <> ();
            for (Subtask subtask:subtasks){
                targetIds.add(subtask.getTargetNodeId());
            }
            List<NginxServiceConfigPhp> targetNodes=new ArrayList<>() ;
            for (String targetId:targetIds){
                NginxServiceConfigPhp targetNode=nginxServiceConfigPhpService.queryNginxServiceConfigPhpById(targetId);
                targetNodes.add(targetNode);
            }
            linkedHashMap.put("targetNodes",targetNodes);
            ////////////////////////获取任务涉及的node区间
        }
        if(taskType.equals("updateTomcatCodeTask")){
            ////////////////////////获取任务内容区间
            UpdateTomcatCodeTask task=updateTomcatCodeTaskService.queryUpdateTomcatCodeTaskById(taskId);
            linkedHashMap.put("task",task);
            ////////////////////////获取任务内容区间
            ////////////////////////获取任务涉及模块存在的node的区间
            List allNodes=tomcatServiceConfigService.queryTomcatServiceConfigsByProjectModuleId(task.getProjectModule());
            linkedHashMap.put("allNodes",allNodes);
            ////////////////////////获取任务涉及模块存在的node的区间
            ////////////////////////获取任务涉及的node区间
            List<Subtask> subtasks =subtaskService.queryNeedDoSubtasksByMainTaskId(taskId);
            LinkedHashSet<String> targetIds=new LinkedHashSet <> ();
            for (Subtask subtask:subtasks){
                targetIds.add(subtask.getTargetNodeId());
            }
            List<TomcatServiceConfig> targetNodes=new ArrayList<>() ;
            for (String targetId:targetIds){
                TomcatServiceConfig targetNode=tomcatServiceConfigService.queryTomcatServiceConfigById(targetId);
                targetNodes.add(targetNode);
            }
            linkedHashMap.put("targetNodes",targetNodes);
            ////////////////////////获取任务涉及的node区间
        }
        if(taskType.equals("updateTomcatCodeAndHeartbeatTask")){
            ////////////////////////获取任务内容区间
            UpdateTomcatCodeAndHeartbeatTask task=updateTomcatCodeAndHeartbeatTaskService.queryUpdateTomcatCodeAndHeartbeatTaskById(taskId);
            linkedHashMap.put("task",task);
            ////////////////////////获取任务内容区间
            ////////////////////////获取任务涉及模块存在的node的区间
            List allNodes=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigsByProjectModuleId(task.getProjectModule());
            linkedHashMap.put("allNodes",allNodes);
            ////////////////////////获取任务涉及模块存在的node的区间
            ////////////////////////获取任务涉及的node区间
            List<Subtask> subtasks =subtaskService.queryNeedDoSubtasksByMainTaskId(taskId);
            LinkedHashSet<String> targetIds=new LinkedHashSet <> ();
            for (Subtask subtask:subtasks){
                targetIds.add(subtask.getTargetNodeId());
            }
            List<TomcatAndHeartbeatServiceConfig> targetNodes=new ArrayList<>() ;
            for (String targetId:targetIds){
                TomcatAndHeartbeatServiceConfig targetNode=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigById(targetId);
                targetNodes.add(targetNode);
            }
            linkedHashMap.put("targetNodes",targetNodes);
            ////////////////////////获取任务涉及的node区间
        }
        //System.out.println(gson.toJson(linkedHashMap));
        response.getWriter().write(gson.toJson(linkedHashMap));
    }
    protected void getUpdatePhpCodeTasks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        List<UpdatePhpCodeTask> updatePhpCodeTasks=updatePhpCodeTaskService.queryUpdatePhpCodeTasks();
        LinkedHashMap updatePhpCodeTaskMap=new LinkedHashMap();
        for (UpdatePhpCodeTask updatePhpCodeTask:updatePhpCodeTasks){
            updatePhpCodeTaskMap.put(updatePhpCodeTask.getId(),updatePhpCodeTask.getName());
        }
        Gson gson=new Gson();
        String mapToJson=gson.toJson(updatePhpCodeTaskMap);
        response.getWriter().write(mapToJson);
    }
    protected void getTaskNodes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String taskId=request.getParameter("taskId");
        String taskType=request.getParameter("taskType");
        if(taskType.equals("updatePhpCodeTask")){
            UpdatePhpCodeTask updatePhpCodeTask=updatePhpCodeTaskService.queryUpdatePhpCodeTaskById(taskId);
            if (updatePhpCodeTask==null){
                response.getWriter().write("没有这个updatePhpCodeTaskId:"+taskId);
                return;
            }else {
                List<Subtask> subtasks=subtaskService.querySubtasksByMainTaskId(updatePhpCodeTask.getId());
                System.out.println(subtasks+"子任务");
                LinkedHashMap linkedHashMap=new LinkedHashMap();
                linkedHashMap.put("mainTaskId",updatePhpCodeTask.getId());
                for (Subtask subtask:subtasks){
                    linkedHashMap.put(subtask.getTargetNodeId(),nginxServiceConfigPhpService.queryNginxServiceConfigPhpById(subtask.getTargetNodeId()).getUse());
                }
                String mapToJson=gson.toJson(linkedHashMap);
                response.getWriter().write(mapToJson);
            }
        }
        if(taskType.equals("updateTomcatCodeTask")){
            UpdateTomcatCodeTask updateTomcatCodeTask=updateTomcatCodeTaskService.queryUpdateTomcatCodeTaskById(taskId);
            if (updateTomcatCodeTask==null){
                response.getWriter().write("没有这个updateTomcatCodeTaskId:"+taskId);
                return;
            }else {
                List<Subtask> subtasks=subtaskService.querySubtasksByMainTaskId(updateTomcatCodeTask.getId());
                System.out.println(subtasks+"子任务");
                LinkedHashMap linkedHashMap=new LinkedHashMap();
                linkedHashMap.put("mainTaskId",updateTomcatCodeTask.getId());
                for (Subtask subtask:subtasks){
                    linkedHashMap.put(subtask.getTargetNodeId(),tomcatServiceConfigService.queryTomcatServiceConfigById(subtask.getTargetNodeId()).getUse());
                }
                String mapToJson=gson.toJson(linkedHashMap);
                response.getWriter().write(mapToJson);
            }
        }
        if(taskType.equals("updateTomcatAndHeartbeatCodeTask")){
            UpdateTomcatCodeTask updateTomcatCodeTask=updateTomcatCodeTaskService.queryUpdateTomcatCodeTaskById(taskId);
            if (updateTomcatCodeTask==null){
                response.getWriter().write("没有这个updateTomcatCodeTaskId:"+taskId);
                return;
            }else {
                List<Subtask> subtasks=subtaskService.querySubtasksByMainTaskId(updateTomcatCodeTask.getId());
                //System.out.println(subtasks+"子任务");
                LinkedHashMap linkedHashMap=new LinkedHashMap();
                linkedHashMap.put("mainTaskId",updateTomcatCodeTask.getId());
                for (Subtask subtask:subtasks){
                    linkedHashMap.put(subtask.getTargetNodeId(),tomcatServiceConfigService.queryTomcatServiceConfigById(subtask.getTargetNodeId()).getUse());
                }
                String mapToJson=gson.toJson(linkedHashMap);
                response.getWriter().write(mapToJson);
            }
        }
    }
    protected void getSpeciallyAppointedTargetNodeMainTaskResult(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String mainTaskId=request.getParameter("mainTaskId");
        String targetNodeId=request.getParameter("targetNodeId");
        List<Subtask> subtasks=subtaskService.getSpeciallyAppointedTargetNodeMainTaskResult(targetNodeId,mainTaskId);
        LinkedHashMap linkedHashMap=new LinkedHashMap();
        for(Subtask subtask:subtasks){
            //LinkedHashMap commandAndResult=new LinkedHashMap();
            //commandAndResult.put(subtask.getCommand(),subtask.getResult());
            linkedHashMap.put(subtask.getId(),subtask);
        }
        response.getWriter().write(gson.toJson(linkedHashMap));
    }
    protected void deleteTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String taskId=request.getParameter("taskId");
        String taskType=request.getParameter("taskType");

        if(Objects.equals(taskType, "updatePhpCodeTask")){
            UpdatePhpCodeTask updatePhpCodeTask=updatePhpCodeTaskService.queryUpdatePhpCodeTaskById(taskId);
            if(updatePhpCodeTask==null){}else {
                updatePhpCodeTaskService.deleteUpdatePhpCodeTaskById(taskId);
                subtaskService.deleteSubtaskByMainTaskId(taskId);
                response.getWriter().write("删除成功");
                return;
            }
//            List<UpdatePhpCodeTask> updatePhpCodeTasks=updatePhpCodeTaskService.queryUpdatePhpCodeTasks();
//            LinkedHashMap updatePhpCodeTaskMap=new LinkedHashMap();
//            for (UpdatePhpCodeTask updatePhpCodeTaskItem:updatePhpCodeTasks){
//                updatePhpCodeTaskMap.put(updatePhpCodeTaskItem.getId(),updatePhpCodeTaskItem.getName());
//            }
//            Gson gson=new Gson();
//            String mapToJson=gson.toJson(updatePhpCodeTaskMap);
//            response.getWriter().write(mapToJson);
        }
    }
    protected void setMainTaskStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String mainTaskId=request.getParameter("mainTaskId");
        String taskStatus=request.getParameter("taskStatus");
        String mainTaskType=request.getParameter("mainTaskType");

        if(mainTaskId==""){
            response.getWriter().write("没收到 mainTaskId");
            return;
        }
        if(mainTaskType.equals("updatePhpCodeTask")){
            UpdatePhpCodeTask updatePhpCodeTask=updatePhpCodeTaskService.queryUpdatePhpCodeTaskById(mainTaskId);
            if (updatePhpCodeTask==null){
                response.getWriter().write("没收到 指定id的主任务,id:"+mainTaskId);
                return;
            }
            updatePhpCodeTask.setStatus(taskStatus);
            updatePhpCodeTaskService.updateUpdatePhpCodeTask(updatePhpCodeTask);

            subtaskService.setSubtaskStatusByMainTaskId(mainTaskId,taskStatus);
            response.getWriter().write("已将任务状态修改为"+taskStatus);
        }
    }
    protected void taskAssign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        if (request.getMethod().equals("GET")){
            //抽取全部targetNode做成json
            LinkedList<TargetNode> targetNodes=new LinkedList<>();
            ProjectModuleType projectModuleType=new ProjectModuleType();
            //抽取全部phpTargetNode
            List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhps();
            projectModuleType=projectModuleTypeService.queryProjectModuleTypeByName("php");
            for(NginxServiceConfigPhp nginxServiceConfigPhp:nginxServiceConfigPhps){
                TargetNode targetNode=new TargetNode();
                targetNode.setId(nginxServiceConfigPhp.getId());
                targetNode.setName(nginxServiceConfigPhp.getUse());
                targetNode.setTypeId(projectModuleType.getId());
                targetNodes.add(targetNode);
            }

            //抽取全部tomcatNode
            List<TomcatServiceConfig> tomcatServiceConfigs=tomcatServiceConfigService.queryTomcatServiceConfigs();
            projectModuleType=projectModuleTypeService.queryProjectModuleTypeByName("tomcat");
            for(TomcatServiceConfig tomcatServiceConfig:tomcatServiceConfigs){
                TargetNode targetNode=new TargetNode();
                targetNode.setId(tomcatServiceConfig.getId());
                targetNode.setName(tomcatServiceConfig.getUse());
                targetNode.setTypeId(projectModuleType.getId());
                targetNodes.add(targetNode);
            }
            //抽取全部tomcatAndHeartbeatNode
            List<TomcatAndHeartbeatServiceConfig> tomcatAndHeartbeatServiceConfigs=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigs();
            projectModuleType=projectModuleTypeService.queryProjectModuleTypeByName("tomcatAndHeartbeat");
            for(TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig:tomcatAndHeartbeatServiceConfigs){
                TargetNode targetNode=new TargetNode();
                targetNode.setId(tomcatAndHeartbeatServiceConfig.getId());
                targetNode.setName(tomcatAndHeartbeatServiceConfig.getUse());
                targetNode.setTypeId(projectModuleType.getId());
                targetNodes.add(targetNode);
            }
            request.setAttribute("targetNodes",targetNodes);
            System.out.println(targetNodes);
            request.getRequestDispatcher("/pages/client/taskAssignForLhwl.jsp").forward(request,response);
        }else if (request.getMethod().equals("POST")){

        }
    }
    protected void taskAssignForRg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        if (request.getMethod().equals("GET")){
            List<DeployEnv> deployEnvs=deployEnvService.queryDeployEnvs();
            request.setAttribute("deployEnvs",deployEnvs);//环境列表
            DeployEnv firstDeployEnv=deployEnvs.iterator().next();
            List<Project> projects=projectService.queryProjects();
            Project firstProject=projects.iterator().next();
            request.setAttribute("projects",projects);//工程列表
            List<ProjectModule> projectModules=projectModuleService.queryProjectModulesByProjectId(firstProject.getId());
            request.setAttribute("projectModules",projectModules);
            ProjectModule firstProjectModule=projectModules.iterator().next();
            ProjectModuleType projectModuleType=projectModuleTypeService.queryProjectModuleTypeById(firstProjectModule.getProjectModuleTypeId());
            if(projectModuleType.getName().equals("php")){
                List<NginxServiceConfigPhp> nginxServiceConfigPhps=nginxServiceConfigPhpService.queryNginxServiceConfigPhpsByProjectModuleIdAndDeployEnvId(firstProjectModule.getId(),firstDeployEnv.getId());
                request.setAttribute("targetNodes",nginxServiceConfigPhps);
            }
            if(projectModuleType.getName().equals("tomcat")){
                List<TomcatServiceConfig> tomcatServiceConfigs=tomcatServiceConfigService.queryTomcatServiceConfigsByProjectModuleIdAndDeployEnvId(firstProjectModule.getId(),firstDeployEnv.getId());
                request.setAttribute("targetNodes",tomcatServiceConfigs);
            }
            if(projectModuleType.getName().equals("tomcatAndHeartbeat")){
                List<TomcatAndHeartbeatServiceConfig> tomcatAndHeartbeatServiceConfigs=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigsByProjectModuleIdAndDeployEnvId(firstProjectModule.getId(),firstDeployEnv.getId());
                request.setAttribute("targetNodes",tomcatAndHeartbeatServiceConfigs);
            }
            request.setAttribute("deployGoals",deployGoalService.queryDeployGoals());

            request.getRequestDispatcher("/pages/client/taskAssignForRg.jsp").forward(request,response);
        }else if (request.getMethod().equals("POST")){

        }
    }
    protected void taskAssignAddTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String deployGoalName=request.getParameter("deployGoalName");
        String deployGoalId=request.getParameter("deployGoalId");
        String deployEnvId=request.getParameter("deployEnvId");
        String projectId=request.getParameter("projectId");
        String projectModuleId=request.getParameter("projectModuleId");
        String targetNodeId=request.getParameter("targetNodeId");
        String fileUrl=request.getParameter("fileUrl");
        String deployServiceModuleId=request.getParameter("deployServiceModuleId");
        String lastId=request.getParameter("lastId");

        if(deployGoalId==null){//创建总任务
            if (deployServiceModuleId==null) {//新建部署模块
                //存储总任务号
                DeployGoal deployGoal=new DeployGoal(WebUtils.id(),deployGoalName,"make");
                deployGoalService.addDeployGoal(deployGoal);
                //存储模块任务号
                DeployServiceModule deployServiceModule=new DeployServiceModule(WebUtils.id(),deployGoal.getId(),deployEnvId,projectId,projectModuleId,targetNodeId,fileUrl,"make","0");
                deployServiceModuleService.addDeployServiceModule(deployServiceModule);
                LinkedHashMap linkedHashMap=new LinkedHashMap();
                linkedHashMap.put("status","添加成功");
                linkedHashMap.put("deployGoalId",deployGoal.getId());
                linkedHashMap.put("deployServiceModuleId",deployServiceModule.getId());
                response.getWriter().write(gson.toJson(linkedHashMap));return;
            }else{
                //没有总任务,所以一定不会有部署模块号
            }
        }else {//追加总任务
            if (deployServiceModuleId==null){//有总任务号,新建部署模块
                DeployServiceModule deployServiceModule=new DeployServiceModule(WebUtils.id(),deployGoalId,deployEnvId,projectId,projectModuleId,targetNodeId,fileUrl,"make",lastId);
                deployServiceModuleService.addDeployServiceModule(deployServiceModule);
                LinkedHashMap linkedHashMap=new LinkedHashMap();
                linkedHashMap.put("status","添加成功");
                linkedHashMap.put("deployGoalId",deployGoalId);
                linkedHashMap.put("deployServiceModuleId",deployServiceModule.getId());
                response.getWriter().write(gson.toJson(linkedHashMap));return;
            }else{//有总任务号,有模块号.修改模块号

                DeployServiceModule deployServiceModule=deployServiceModuleService.queryDeployServiceModuleById(deployServiceModuleId);
                deployServiceModule.setDeployEnvId(deployEnvId);
                deployServiceModule.setProjectId(projectId);
                deployServiceModule.setProjectModuleId(projectModuleId);
                deployServiceModule.setTargetNodeId(targetNodeId);
                deployServiceModule.setFileUrl(fileUrl);
                deployServiceModule.setStatus("make");
                deployServiceModuleService.updateDeployServiceModule(deployServiceModule);
                LinkedHashMap linkedHashMap=new LinkedHashMap();
                linkedHashMap.put("status","修改成功");
                response.getWriter().write(gson.toJson(linkedHashMap));return;
            }
        }
    }
    protected void taskAssignDeleteDeployServiceModuleById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String deployServiceModuleId=request.getParameter("deployServiceModuleId");

        DeployServiceModule deployServiceModule=deployServiceModuleService.queryDeployServiceModuleById(deployServiceModuleId);
        DeployServiceModule deployServiceModuleNext=deployServiceModuleService.queryDeployServiceModuleByLastId(deployServiceModule.getId());
        if (deployServiceModuleNext==null){}else {
            deployServiceModuleNext.setLastId(deployServiceModule.getLastId());
            deployServiceModuleService.updateDeployServiceModule(deployServiceModuleNext);
        }
        //执行递归行为,处理lastId记录
        deployServiceModuleService.deleteDeployServiceModuleById(deployServiceModuleId);
        response.getWriter().write("已删除");
    }
    protected void taskAssignGetDeployServiceModulesByDeployGoalId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String deployGoalId=request.getParameter("deployGoalId");
        List<DeployServiceModule> deployServiceModules=deployServiceModuleService.queryDeployServiceModulesByDeployGoalId(deployGoalId);
        response.getWriter().write(gson.toJson(deployServiceModules));
    }
    protected void taskAssignStartOrStopDeployServiceModuleById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String deployServiceModuleId=request.getParameter("deployServiceModuleId");
        DeployServiceModule deployServiceModule=deployServiceModuleService.queryDeployServiceModuleById(deployServiceModuleId);
        ProjectModule projectModule=projectModuleService.queryProjectModuleById(deployServiceModule.getProjectModuleId());
        ProjectModuleType projectModuleType=projectModuleTypeService.queryProjectModuleTypeById(projectModule.getProjectModuleTypeId());
        DeployGoal deployGoal=deployGoalService.queryDeployGoalById(deployServiceModule.getDeployGoalId());

        if(projectModuleType.getName().equals("php")){

        }
        if(projectModuleType.getName().equals("tomcat")){

        }
        if(projectModuleType.getName().equals("tomcatAndHeartbeat")){
            TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigById(deployServiceModule.getTargetNodeId());
            //下线
            NginxServiceConfigProxy nginxServiceConfigProxy=nginxServiceConfigProxyService.queryNginxServiceConfigProxyByTargetNodeId(tomcatAndHeartbeatServiceConfig.getId());
            String nginxConfigFile="/lhwl/app/nginx/conf/vhost/"+nginxServiceConfigProxy.getFrontDomain()+".conf";

            Server server=serverService.queryServerById(tomcatAndHeartbeatServiceConfig.getServerId());
            String serverIp=server.getIp();
            String tomcatPort=tomcatAndHeartbeatServiceConfig.getHttpPort();
            String command="grep -n "+serverIp+":"+tomcatPort+" "+nginxConfigFile;
            MainTask mainTask=new MainTask(WebUtils.id(),deployServiceModuleId,deployGoal.getId(),"offline","make","0");
            mainTaskService.addMainTask(mainTask);
            BasicBehavior basicBehavior=new BasicBehavior(WebUtils.id(),mainTask.getId(),deployServiceModuleId,deployGoal.getId(),command,server.getId(),"","make","grep");
            basicBehaviorService.addBasicBehavior(basicBehavior);
            response.getWriter().write("已开始");

            //停止tomcat服务
            //备份
            //下载war包,解压,启动,检测
            //决定是否上线
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(Thread.currentThread().getStackTrace()[1].getFileName()+":"
                +Thread.currentThread().getStackTrace()[1].getLineNumber()+":"
                +"已经进入doget方法,"+request.getParameter("action")+" 是action名");
        doPost(request,response);//把dopost方法优化到baseServlet抽象类中去了,使得可以按照request action执行特定的方法
        //由于action判断行为在dopost中,所以在doget中执行一下dopost就可以执行判断了,从而执行特定的方法了.
    }

    public String offline(BasicBehavior lastBasicBehavior){//(理论上,应该在这里对 "主任务" 拆分)
        String yesOrNo="";
        //String result=lastBasicBehavior.getResult().trim();
        //System.out.println(result);
        BasicBehavior basicBehavior=new BasicBehavior();
        basicBehavior.setId(WebUtils.id());
        basicBehavior.setMainTaskId(lastBasicBehavior.getMainTaskId());
        basicBehavior.setDeployServiceModuleId(lastBasicBehavior.getDeployServiceModuleId());
        basicBehavior.setDeployGoalId(lastBasicBehavior.getDeployGoalId());
        basicBehavior.setClientId(lastBasicBehavior.getClientId());

        if (lastBasicBehavior.getType().equals("grep")){
            //说明上一条执行成功了,获取它的结果,为下一条操作做准备
            String result=lastBasicBehavior.getResult().replace("\n","").replaceAll("\r","");
            //System.out.println(result);

            String line=result.substring(0,result.indexOf(":"));
            String contextOld=result.substring(result.indexOf(":")+1,result.length());
            //System.out.println(line);
            //System.out.println(contextOld);
            String contextNew=contextOld.replace(" down","").replace(";"," down;");
            //System.out.println(contextNew);
            String commandLast=lastBasicBehavior.getCommand();
            String file=commandLast.substring(commandLast.lastIndexOf(" ")+1,commandLast.length());

            String command="sed -i  '"+line+ "c \\" +contextNew+ "' " +file;

            basicBehavior.setCommand(command);
            basicBehavior.setStatus("make");
            basicBehavior.setType("sed");
            basicBehaviorService.addBasicBehavior(basicBehavior);
            yesOrNo="yes";
        }
        if (lastBasicBehavior.getType().equals("sed")){
            //说明上一条执行成功了,获取它的状态,成功则继续;不用看,它一定是成功的,否则进不到这里来
            String command="/etc/init.d/nginx reload";
            basicBehavior.setCommand(command);
            basicBehavior.setStatus("make");
            basicBehavior.setType("reload");
            basicBehaviorService.addBasicBehavior(basicBehavior);
            yesOrNo="yes";
        }
        if (lastBasicBehavior.getType().equals("reload")){
            yesOrNo="no";
        }
        return yesOrNo;
    }//(理论上,应该在这里对 "主任务" 拆分)
    public String pull(BasicBehavior lastBasicBehavior){// 说明pull任务完成了(理论上,应该在这里对 "主任务" 拆分)
        String yesOrNo="";
        if (lastBasicBehavior.getType().equals("pull")){//基础行为为pull 环节也为pull
            if (lastBasicBehavior.getResult().equals("success")) {//这个成功是api给的,是程序心跳api给的
                //System.out.println(lastBasicBehavior.getResult());
                yesOrNo="no";//本环节完事了
            }else {
                yesOrNo="error";
            }

        }
        return yesOrNo;
    }//(理论上,应该在这里对 "主任务" 拆分)
    public String online(BasicBehavior lastBasicBehavior){//你现在执行的主任务叫online
        String yesOrNo="";
        //String result=lastBasicBehavior.getResult().trim();
        //System.out.println(result);
        BasicBehavior basicBehavior=new BasicBehavior();
        basicBehavior.setId(WebUtils.id());
        basicBehavior.setMainTaskId(lastBasicBehavior.getMainTaskId());
        basicBehavior.setDeployServiceModuleId(lastBasicBehavior.getDeployServiceModuleId());
        basicBehavior.setDeployGoalId(lastBasicBehavior.getDeployGoalId());
        basicBehavior.setClientId(lastBasicBehavior.getClientId());

        if (lastBasicBehavior.getType().equals("grep")){//这个主任务的上一个子任务是grep,那么下一个子任务就是sed
            //说明上一条执行成功了,获取它的结果,为下一条操作做准备
            String result=lastBasicBehavior.getResult().replace("\n","").replaceAll("\r","");
            //System.out.println(result);

            String line=result.substring(0,result.indexOf(":"));
            String contextOld=result.substring(result.indexOf(":")+1,result.length());
            //System.out.println(line);
            //System.out.println(contextOld);
            String contextNew=contextOld.replace(" down","");
            //System.out.println(contextNew);
            String commandLast=lastBasicBehavior.getCommand();
            String file=commandLast.substring(commandLast.lastIndexOf(" ")+1,commandLast.length());

            String command="sed -i  '"+line+ "c \\" +contextNew+ "' " +file;

            basicBehavior.setCommand(command);
            basicBehavior.setStatus("make");
            basicBehavior.setType("sed");
            basicBehaviorService.addBasicBehavior(basicBehavior);
            yesOrNo="yes";
        }
        if (lastBasicBehavior.getType().equals("sed")){
            //说明上一条执行成功了,获取它的状态,成功则继续;不用看,它一定是成功的,否则进不到这里来
            String command="/etc/init.d/nginx reload";
            basicBehavior.setCommand(command);
            basicBehavior.setStatus("make");
            basicBehavior.setType("reload");
            basicBehaviorService.addBasicBehavior(basicBehavior);
            yesOrNo="yes";
        }
        if (lastBasicBehavior.getType().equals("reload")){
            yesOrNo="no";
        }
        return yesOrNo;
    }

    public String tomcatAndHeartbeat(MainTask lastMainTask){
        String yesOrNo="";

        if(lastMainTask.getName().equals("online")){
            //上一个"主任务是 online ",且运行到这里,说明"结果为ok",那么我决定"从此以后就停止""这种服务形式(tomcatAndHeartbeat)"的更新;换句话说:"就是online,就是最后,一条,我这里就是告诉程序可以终结这种服务的发布活动的操作"
            yesOrNo="no";
        }else{
            //环节 公共部分
            MainTask mainTask=new MainTask();
            mainTask.setId(WebUtils.id());
            mainTask.setDeployServiceModuleId(lastMainTask.getDeployServiceModuleId());
            mainTask.setDeployGoalId(lastMainTask.getDeployGoalId());
            mainTask.setLastId(lastMainTask.getId());
            mainTask.setStatus("make");

            BasicBehavior basicBehavior=new BasicBehavior();
            basicBehavior.setId(WebUtils.id());
            basicBehavior.setMainTaskId(mainTask.getId());
            basicBehavior.setDeployServiceModuleId(mainTask.getDeployServiceModuleId());
            basicBehavior.setDeployGoalId(mainTask.getDeployGoalId());
            basicBehavior.setResult("");
            basicBehavior.setStatus("make");
            //环节 公共部分
            if(lastMainTask.getName().equals("offline")){
                mainTask.setName("pull");
                //定义这个主任务的第一个子任务
                List<BasicBehavior> basicBehaviors=basicBehaviorService.queryBasicBehaviorsByMainTaskId(lastMainTask.getId());
                basicBehavior.setClientId(basicBehaviors.iterator().next().getClientId());
                DeployServiceModule deployServiceModule=deployServiceModuleService.queryDeployServiceModuleById(mainTask.getDeployServiceModuleId());
                ProjectModule projectModule=projectModuleService.queryProjectModuleById(deployServiceModule.getProjectModuleId());
                String deployPullRootUrl=projectModule.getDeployPullRootUrl();
                TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigById(deployServiceModule.getTargetNodeId());
                String sourceWarUrl=WebUtils.addSeparatorChar(deployPullRootUrl)+deployServiceModule.getFileUrl();
                String tomcatHome=WebUtils.addSeparatorChar(tomcatAndHeartbeatServiceConfig.getTomcatHome());
                String targetWarPath=tomcatHome+"webapps/"+deployServiceModule.getFileUrl();
                String tomcatWorkDir=tomcatHome+"webapps/ROOT";
                String httpPort=tomcatAndHeartbeatServiceConfig.getHttpPort();
                String heartbeatUrl=tomcatAndHeartbeatServiceConfig.getHeartbeat();
                basicBehavior.setCommand("wget "+sourceWarUrl+" -O "+targetWarPath+";" +
                        tomcatHome+"bin/catalina.sh stop;sleep 10;"+
                        "mv "+tomcatWorkDir+"{,_"+basicBehavior.getId()+"};"+
                        "mkdir -p "+tomcatHome+"webapps/ROOT;"+
                        "unzip -q "+targetWarPath+" -d "+tomcatWorkDir+"/;"+
                        "rm -f "+targetWarPath+";"+
                        tomcatHome+"bin/catalina.sh start;sleep 15;" +
                        "curl http://127.0.0.1:"+httpPort+"/"+heartbeatUrl);

                basicBehavior.setType("pull");
            }
            if(lastMainTask.getName().equals("pull")){
                mainTask.setName("online");
                //定义这个主任务的第一个子任务
                List<BasicBehavior> basicBehaviors=basicBehaviorService.queryBasicBehaviorsByMainTaskId(lastMainTask.getId());
                basicBehavior.setClientId(basicBehaviors.iterator().next().getClientId());
                //basicBehavior.setClientId(WebUtils.id());
                DeployServiceModule deployServiceModule=deployServiceModuleService.queryDeployServiceModuleById(lastMainTask.getDeployServiceModuleId());
                ProjectModule projectModule=projectModuleService.queryProjectModuleById(deployServiceModule.getProjectModuleId());
                ProjectModuleType projectModuleType=projectModuleTypeService.queryProjectModuleTypeById(projectModule.getProjectModuleTypeId());
                String command="";
                if(projectModuleType.getName().equals("tomcatAndHeartbeat")){
                    TomcatAndHeartbeatServiceConfig tomcatAndHeartbeatServiceConfig=tomcatAndHeartbeatServiceConfigService.queryTomcatAndHeartbeatServiceConfigById(deployServiceModule.getTargetNodeId());
                    Server server=serverService.queryServerById(tomcatAndHeartbeatServiceConfig.getServerId());
                    String ip=server.getIp();
                    String httpPort=tomcatAndHeartbeatServiceConfig.getHttpPort();
                    NginxServiceConfigProxy nginxServiceConfigProxy=nginxServiceConfigProxyService.queryNginxServiceConfigProxyByTargetNodeId(tomcatAndHeartbeatServiceConfig.getId());
                    String domain=nginxServiceConfigProxy.getFrontDomain();
                    String nginxVhostConfigFile="/lhwl/app/nginx/conf/vhost/"+domain+".conf";
                    command="grep -n "+ip+":"+httpPort+" "+nginxVhostConfigFile;
                }
                basicBehavior.setCommand(command);
                basicBehavior.setType("grep");
            }

            basicBehaviorService.addBasicBehavior(basicBehavior);
            mainTaskService.addMainTask(mainTask);
            yesOrNo="yes";
        }
        return  yesOrNo;
    }

}
